package com.bytetool.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bytetool.server.entity.MethodCallStackInfo;
import com.bytetool.server.entity.PerformanceInfo;
import com.bytetool.server.entity.PerformanceRequest;
import com.bytetool.server.mapper.PerformanceInfoMapper;
import com.bytetool.server.service.IPerformanceInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xweb.common.utils.PageUtil;
import com.xweb.common.utils.StringUtils;
import com.xweb.common.web.standard.PageEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhx
 * @since 2024-04-01
 */
@Service
public class PerformanceInfoServiceImpl extends ServiceImpl<PerformanceInfoMapper, PerformanceInfo> implements IPerformanceInfoService {

    @Override
    public PageEntity<MethodCallStackInfo> listMethodCallStackInfo(PerformanceRequest performanceRequest) {
        PageUtil.startPage(performanceRequest);
        QueryWrapper queryWrapper = new QueryWrapper<PerformanceInfo>();
        List<PerformanceInfo> dataList = list(queryWrapper);
        return PageUtil.buildResult(buildMethodCallStackList(dataList));
    }


    private List<MethodCallStackInfo> buildMethodCallStackList(List<PerformanceInfo> performanceInfos) {
        List<MethodCallStackInfo> resultList = new ArrayList<>();

        Map<String, List<PerformanceInfo>> traceGroups = performanceInfos.stream().collect(Collectors.groupingBy(PerformanceInfo::getTraceId));
        Set<String> keys = traceGroups.keySet();

        for (String key : keys) {
            MethodCallStackInfo methodCallStackInfo = buildMethodCallStack(traceGroups.get(key));
            resultList.add(methodCallStackInfo);
        }

        return resultList;
    }

    private MethodCallStackInfo buildMethodCallStack(List<PerformanceInfo> performanceInfos) {
        Map<String, List<PerformanceInfo>> parentMethodMap = performanceInfos.stream().collect(Collectors.groupingBy(PerformanceInfo::getParentMethodName));
        MethodCallStackInfo methodCallStackInfo = new MethodCallStackInfo();
        PerformanceInfo performanceInfo = parentMethodMap.get("").get(0);
        methodCallStackInfo.setMethodName(performanceInfo.getMethodName());
        methodCallStackInfo.setChildren(buildSubMethodCallStack(parentMethodMap, performanceInfo.getMethodName()));
        return methodCallStackInfo;
    }

    private List<MethodCallStackInfo> buildSubMethodCallStack(Map<String, List<PerformanceInfo>> parentMethodMap, String parentMethodName) {
        List<MethodCallStackInfo> result = new ArrayList<>();
        List<PerformanceInfo> performanceInfos = parentMethodMap.get(parentMethodName);
        if (null == performanceInfos) {
            return result;
        }
        for (PerformanceInfo performanceInfo : performanceInfos) {
            MethodCallStackInfo methodCallStackInfo = new MethodCallStackInfo();
            methodCallStackInfo.setMethodName(performanceInfo.getMethodName());
            methodCallStackInfo.setSpend(performanceInfo.getSpend());
            methodCallStackInfo.setChildren(buildSubMethodCallStack(parentMethodMap, performanceInfo.getMethodName()));
            result.add(methodCallStackInfo);
        }
        return result;
    }

}
